package com.yc.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.dto.request.AvatarsActionCreateParam;
import com.yc.cloud.admin.service.AvatarsActionService;
import com.yc.cloud.admin.vo.AvatarsActionVo;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.AvatarsAction;
import com.yc.cloud.mapper.AvatarsActionMapper;
import com.yc.cloud.oss.service.OssFileService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数字人动画 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-03-29
 */
@Service
public class AvatarsActionServiceImpl extends ServiceImpl<AvatarsActionMapper, AvatarsAction> implements AvatarsActionService {

    private final OssFileService ossFileService;

    public AvatarsActionServiceImpl(OssFileService ossFileService) {
        this.ossFileService = ossFileService;
    }

    @Override
    public IPage<AvatarsActionVo> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<AvatarsAction>();
        IPage<AvatarsAction> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
//        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
//            query.and(queryWrapper -> {
//
//            });
//        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val result = baseMapper.selectPage(page, query);
        return result.convert(this::convertVo);
    }

    @Override
    public AvatarsAction create(AvatarsActionCreateParam param) {
        val avatarsaction = new AvatarsAction();
        BeanUtils.copyProperties(param, avatarsaction);
        val saveFlag = save(avatarsaction);
        return saveFlag ? avatarsaction : null;
    }

    @Override
    public AvatarsAction get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public AvatarsAction update(Long id, AvatarsAction param) {
        val avatarsaction = new AvatarsAction();
        BeanUtils.copyProperties(param, avatarsaction);
        avatarsaction.setId(id);
        val updateFlag = updateById(avatarsaction);
        return updateFlag ? avatarsaction : null;
    }


    @Override
    public AvatarsActionVo convertVo(AvatarsAction avatarsaction) {
        if (avatarsaction == null) {
            return null;
        }
        val vo = new AvatarsActionVo();
        BeanUtils.copyProperties(avatarsaction, vo);
        vo.setUrl(ossFileService.getOssFileUrl(String.valueOf(avatarsaction.getFileId())));
        return vo;
    }

    @Override
    public List<AvatarsActionVo> convertVo(List<AvatarsAction> AvatarsActionList) {
        if (CollUtil.isNotEmpty(AvatarsActionList)) {
            return AvatarsActionList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
